<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\Course\CourseDetailsResource;
use App\Http\Resources\Course\CourseListResource;
use App\Http\Resources\Course\CourseOrderResource;
use App\Services\CourService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CourController extends Controller
{
    public function list(Request $request, CourService $service)
    {
        $classList = $service->classList();
        $list = $service->list($this->getMiniType(), $request->input('class_id'));

        return $this->_success([
            'class_list' => $classList,
            'list'       => CourseListResource::collection($list)
        ]);
    }

    public function details(int $id, CourService $service)
    {
        if (auth('api')->check()) {
            $memberId = auth('api')->user()->id;
        }

        return $this->_success(['info' => new CourseDetailsResource($service->details($id, $memberId ?? 0))]);
    }

    public function watchReport(Request $request, CourService $service)
    {
        $data = Validator::make($request->post(), [
            'course_id'       => 'required|integer',
            'course_video_id' => 'required|integer',
            'pace'            => 'nullable|integer',
        ], [
            'course_id.required'       => '课程ID不能为空',
            'course_id.integer'        => '课程ID类型错误',
            'course_video_id.required' => '课程视频ID不能为空',
            'course_video_id.integer'  => '课程视频ID类型错误',
            'pace.integer'             => '进度类型错误',
        ])->validate();

        $service->watchHistory(auth('api')->user()->id, $data['course_id'], $data['course_video_id'], $data['pace'] ?? 0);

        return $this->success('success');
    }

    public function collect(int $id, CourService $service)
    {
        $service->collect(auth('api')->user()->id, $id);

        return $this->success('操作成功');
    }

    public function comment(Request $request, CourService $service)
    {
        $data = Validator::make($request->post(), [
            'course_id' => 'required|integer',
            'content'   => 'required|string|max:255',
        ], [
            'course_id.required' => '课程ID不能为空',
            'course_id.integer'  => '课程ID类型错误',
            'content.required'   => '评论内容不能为空',
            'content.string'     => '评论内容类型错误',
            'content.max'        => '评论内容不能超过255个字符',
        ])->validate();

        $service->comment(auth('api')->user()->id, $data['course_id'], $data['content']);

        return $this->success('操作成功');
    }

    public function source(Request $request, CourService $service)
    {
        $data = Validator::make($request->input(), [
            'course_id'       => 'required|integer',
            'course_video_id' => 'required|integer',
        ], [
            'course_id.required'       => '课程ID不能为空',
            'course_id.integer'        => '课程ID类型错误',
            'course_video_id.required' => '课程视频ID不能为空',
            'course_video_id.integer'  => '课程视频ID类型错误',
        ])->validate();

        return $this->_success(['info' => $service->getSource($data['course_id'], $data['course_video_id'], auth('api')->user()->id)]);
    }

    public function unifiedOrder(Request $request, CourService $service)
    {
        $data = Validator::make($request->post(), [
            'course_id' => 'required|integer',
        ], [
            'course_id.required' => '课程ID不能为空',
            'course_id.integer'  => '课程ID类型错误',
        ])->validate();

        return $this->_success($service->unifiedOrder($data['course_id'], auth('api')->user()->id, $this->getMiniType()));
    }

    public function memberOrder(Request $request, CourService $service)
    {
        $list = $service->memberOrder(auth('api')->user()->id);

        return $this->_success(['list' => CourseOrderResource::collection($list)]);
    }

    public function collectList(CourService $service)
    {
        $list = $service->collectList(auth('api')->user()->id);
        return $this->_success(['list' => $list->items()]);
    }

    public function paidNotify(string $sn, CourService $service)
    {
        return $service->paidNotify($sn);
    }

    public function refundNotify(Request $request, string $sn, CourService $service)
    {
        return $service->refundNotify($sn);
    }
}
